package com.wlyuan.open.scanner.domain.finance;

import com.wlyuan.open.scanner.dao.entity.BalanceBillindexDO;
import com.wlyuan.open.scanner.utils.JsonUtils;
import com.wlyuan.open.scanner.utils.mappings.BaseMapping;
import com.wlyuan.open.scanner.utils.mappings.MappingInvalid;
import com.wlyuan.open.utils.LongUtils;
import lombok.var;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Optional;

/**
 * @author lijing
 * @description
 */
public class BillConvert {

    private static final Convert CONVERT = Mappers.getMapper(Convert.class);

    @Mapper(uses = MappingInvalid.class)
    public interface Convert {

        @Mappings({
                @Mapping(target = "id", source = "fbId"),
                @Mapping(target = "sourceKey", source = "indexId"),
                @Mapping(target = "tenantId", source = "indexOwnercompanyid"),
                @Mapping(target = "createTime", source = "indexInserttime"),
                @Mapping(target = "updateTime", source = "indexUpdatetime"),
                @Mapping(target = "createBy", source = "indexAuthor"),
                @Mapping(target = "updateBy", source = "indexUpdater"),
                @Mapping(target = "name", source = "indexBname"),
                @Mapping(target = "desc", source = "indexDescription"),
                @Mapping(target = "billType", source = "indexBilltype"),
                @Mapping(target = "billNo", source = "indexBcode"),

                @Mapping(target = "invoice.invoicingDate", source = "indexBillingdate"),
                @Mapping(target = "invoice.invoiceCode", source = "indexCode"),
                @Mapping(target = "invoice.invoiceNumber", source = "indexNo"),
                @Mapping(target = "invoice.invoiceMoney", source = "indexAmount"),
                @Mapping(target = "invoice.desc", source = "indexBillingdesc"),
        })
        @BaseMapping
        Bill convert(BalanceBillindexDO source);

        @AfterMapping
        default void after(@MappingTarget Bill target, BalanceBillindexDO source) {
            target.setRaw(JsonUtils.withNullJson(source));
        }

        List<Bill> convert(List<BalanceBillindexDO> list);

        @Mappings({
                @Mapping(target = "tenantId", source = "upTenantId"),
                @Mapping(target = "status", source = "indexStatus"),
                @Mapping(target = "driverId", source = "indexDriverid"),
        })
        BillUp upConvert(BalanceBillindexDO source);

        @Mappings({
                @Mapping(target = "customerId", source = "indexCustomerid"),
                @Mapping(target = "tenantId", source = "downTenantId"),
                @Mapping(target = "status", source = "indexStatus"),
        })
        BillDown downConvert(BalanceBillindexDO source);

    }

    public static List<Bill> convert(List<BalanceBillindexDO> creation) {
        return CONVERT.convert(creation);
    }

    public static Bill convert(BalanceBillindexDO source, Long down) {
        var convert = CONVERT.convert(source);
        Optional.ofNullable(convert.getEcho()).ifPresent(h -> {
            h.put("upKey", source.getIndexId());
            if (LongUtils.notNullLong(down)) {
                h.put("downKey", down);
            }
        });
        if (source.getIndexBillingstatus() != 1) {
            convert.setInvoice(null);
        }
        return convert;
    }

    public static BillUp upConvert(BalanceBillindexDO source) {
        return CONVERT.upConvert(source);
    }

    public static BillDown downConvert(BalanceBillindexDO source) {
        return CONVERT.downConvert(source);
    }
}
